package com.hunan.api.catv.action.openApi;

import com.hunan.api.catv.action.BaseAction;
import com.hunan.api.catv.common.Api;
import com.hunan.api.catv.entity.CodeResult;
import com.hunan.api.catv.entity.Result;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.dto.ExternalPushDTO;
import com.hunan.api.catv.service.biz.openApi.OpenApiBiz;
import com.hunan.api.catv.utils.GsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author jiang zhuan
 * @version 1.0
 * @description 描述
 * @date 2020/12/15 0015
 * @package com.hunan.api.catv.action.openApi
 * @company 湖南有线
 */
@RequestMapping(Api.openApi)
@RestController
public class OpenApiAction extends BaseAction {

    @Autowired
    OpenApiBiz openApiBiz;

    @GetMapping("/cdncheck")
    public CodeResult cdnCheck(String randomCheckCode, HttpServletRequest request) {
        log.info("鉴权接口，参数：[{}]-[{}]-[{}]", randomCheckCode, getRemoteIp(request), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHssmm")));
        CodeResult codeResult = openApiBiz.cdnCheck(randomCheckCode);
        log.info("鉴权结果：{}", GsonUtils.toJson(codeResult));
        return codeResult;
    }

    @PostMapping("/push/externalPush")
    public Result externalPush(ExternalPushDTO externalPushDTO, HttpServletRequest request) {
        log.info("第三方推送接口，参数：[{}]-[{}]-[{}]", externalPushDTO.toString(), getRemoteIp(request), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHssmm")));
        if (ObjectUtils.isEmpty(externalPushDTO.getMemberId())) {
            return buildRequire("memberId is required");
        }
        if (StringUtils.isBlank(externalPushDTO.getContent())) {
            return buildRequire("content is required");
        }
        ServiceResult codeResult = openApiBiz.externalPush(externalPushDTO);
        log.info("第三方推送结果：{}", GsonUtils.toJson(codeResult));
        return buildOK(codeResult, "");
    }

}
